const {
    ccclass,
    property
} = cc._decorator;

import SystemDataSys from "./SystemDataSys";
import PeripheralSystemDataMgr from "./PeripheralSystemDataMgr";
import EventMgr from "./EventMgr";
import GameEventType from "./GameEventType";
import MatchGameEventType from "./MatchGameEventType";
import frameworkManager from "./frameworkManager";
import AdManager from "./AdManager";
import SdkHelper from "./SdkHelper";
import MatchGameViewCtrl from "./MatchGameViewCtrl";
import UiManage from "./UiManage";
import MatchGameData from "./MatchGameData";
import MatchGameDataMgr from "./MatchGameDataMgr";
import PageMgr from "./PageMgr";
import UiManager from "./UiManage";
import NativeUtils from "./NativeUtils";

@ccclass
export default class newMain extends cc.Component {
    checkNode = null;
    matchGameView = null;

    onLoad() {
        UiManager.loaderView(this.node, "MainUi");
        EventMgr.listen(MatchGameEventType.LOAD_MATCH_SCENE, this.loadMatchGame, this);
        EventMgr.listen(MatchGameEventType.END_MATCH_GAME, this.endMatchGame, this);
        EventMgr.listen(MatchGameEventType.ADD_ALL_FILL, this.addAllFill3, this);
        EventMgr.listen(MatchGameEventType.MATCHGAME_RESTE_DATA, this.onResteData, this);
        EventMgr.listen(MatchGameEventType.HIDE_MATCHGAME_NODE, this.hideMatchgameNode, this);
        EventMgr.listen(MatchGameEventType.SHOW_MATCHGAME_NODE, this.showMatchgameNode, this);
        EventMgr.listen("showAdImage",this.showAdImage,this);
        EventMgr.listen("hideAdImage",this.hideAdImage,this);
        
        // AdManager.getInstance().reloadInsertAd();
        PeripheralSystemDataMgr.dealPlayBgMusic(false);
    };

    private showAdImage(pageNumber){
        NativeUtils.showAdImage(pageNumber);
    }

    private hideAdImage(){
        NativeUtils.hideAdImage();
    }

    

    onResteData() {
        this.unscheduleAllCallbacks();
    };

    start() {
        this.scheduleOnce(() => {
            this.dealReviewing();
        }, 0.1);
    };

    loadMatchGame(e) {
        const t = this;
        if (e === undefined) e = false;
        PeripheralSystemDataMgr.dealPlayBgMusic(false, true);
        MatchGameData.allFillArr = [];
        MatchGameData.lastAllFillTime = 0;
        PageMgr.hasShowPage("MatchGameGuidePage") && PageMgr.hidePage("MatchGameGuidePage");
        if (!e) {
            PageMgr.showPage("MatchGameLoadingPage", {
                index: MatchGameData.startGuideType,
                level: MatchGameData.gameLevel
            });
        }
        const a = this.node.getChildByName("MatchGameView");
        if (a) {
            a.active = true;
            a.getComponent(MatchGameViewCtrl).initGameData(null);
            if (e) a.getComponent(MatchGameViewCtrl).playOnloadAni();
        } else {
            UiManage.loaderViewDeepPath(this.node, "MatchGameView", "MatchGame/view", {}, 0).then(() => {
                t.node.getChildByName("MatchGameView").getComponent(MatchGameViewCtrl).initGameData(null);
                t.matchGameView = t.node.getChildByName("MatchGameView");
            });
        }
    };

    hideMatchgameNode() {
        if (this.matchGameView) {
            this.matchGameView.getComponent(MatchGameViewCtrl).setMatchGameCenterActive(false);
        }
    };

    showMatchgameNode() {
        if (this.matchGameView) {
            this.matchGameView.getComponent(MatchGameViewCtrl).setMatchGameCenterActive(true);
        }
    };

    endMatchGame(e) {
        PeripheralSystemDataMgr.dealPlayBgMusic(false);
        if (e && e.is_tg) {
            EventMgr.trigger(GameEventType.PANADA_MOVE);
        } else {
            EventMgr.trigger(GameEventType.SET_ROLL_NOTICE_PARENT);
            EventMgr.trigger(GameEventType.SET_TOP_NODE_CANTOUCH, {
                isCanTouch: true
            });
        }
        EventMgr.trigger(MatchGameEventType.GUIDE_OVER);
        PeripheralSystemDataMgr.dealBreakthroughGuide(e);
        this.node.getChildByName("MatchGameView").getComponent(MatchGameViewCtrl).resetData();
        this.node.getChildByName("MatchGameView").active = false;
    };

    showUnluckPage() {
        const e = MatchGameData.gameLevel;
        const t = MatchGameData.propInfo;
        if (e === 7 && t[0].skillCount > 0) {
            PageMgr.showPage("MatchGameUnlockPropPage", {
                type: t[0].skillType,
                count: t[0].skillCount
            });
        }
        if (e === 13 && t[1].skillCount > 0) {
            PageMgr.showPage("MatchGameUnlockPropPage", {
                type: t[1].skillType,
                count: t[1].skillCount
            });
        }
        if (e === 16 && t[2].skillCount > 0) {
            PageMgr.showPage("MatchGameUnlockPropPage", {
                type: t[2].skillType,
                count: t[2].skillCount
            });
        }
    };

    dealReviewing() {
        if (cc.sys.os === cc.sys.OS_ANDROID && SystemDataSys.auth_type) {
            frameworkManager.log("防沉迷:" + SystemDataSys.auth_type);
            SdkHelper.ysdkLogin();
        }
        if (SdkHelper.getIsCheckUser() && this.checkNode) {
            this.checkNode.active = true;
        }
    };

    addAllFill(e) {
        if (e) {
            MatchGameData.allFillQueue.enqueue(e);
        }
    };

    addAllFill2(e) {
        const t = e.callTime;
        const a = e.tTime;
        const o = t + 1000 * a;
        MatchGameData.allFillArr.push({
            callTime: t,
            tTime: a,
            realTime: o
        });
        MatchGameData.allFillArr.sort((e, t) => {
            return e.realTime - t.realTime;
        });
        frameworkManager.log("allFillArr==", MatchGameData.allFillArr);
    };

    addAllFill3(e) {
        if (e.tTime != 0) {
            MatchGameDataMgr.AllFill();
        } else {
            frameworkManager.log("立马计算逻辑数据");
            MatchGameDataMgr.AllFill();
        }
    };

    lateUpdate() { };

    // 其他属性省略
}